<template>
  <div class="mt-[20px] mb-[30px]">
    <el-form
      ref="ruleFormRef"
      :model="form"
      :rules="formRules"
      class="add-form"
      label-width="120px"
    >
      <el-form-item label="旧密码" prop="oldPwd">
        <el-input v-model="form.oldPwd" show-password placeholder="请输入" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input v-model="form.newPwd" show-password placeholder="请输入" />
      </el-form-item>
      <el-form-item label="确认新密码" prop="newPwd2">
        <el-input v-model="form.newPwd2" show-password placeholder="请输入" />
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
defineOptions({
  name: "ResetPwd"
});

const form = ref({
  oldPwd: "",
  newPwd: "",
  newPwd2: ""
});
const ruleFormRef = ref();
const formRules = {
  oldPwd: [{ required: true, message: "请输入", trigger: "blur" }],
  newPwd: [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入"));
        } else if (value.length < 6) {
          callback(new Error("新密码长度不可小于6位"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ],
  newPwd2: [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请输入"));
        } else if (form.value.newPwd != form.value.newPwd2) {
          callback(new Error("两个输入的新密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ]
};

// 传值
const props = defineProps({
  //  1: 新增 2: 编辑
  id: {
    type: Number,
    default: null
  }
});

function getRef() {
  return ruleFormRef.value;
}

function getFormVal() {
  return form.value;
}

defineExpose({ getRef, getFormVal });
</script>
<style lang="scss" scoped>
.add-form {
  text-align: center;
  .el-form-item {
    width: 90%;
  }
}
</style>
